package com.example.module_motorcircle.motor_circle

import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.Router
import com.example.lib_common.base.BaseFragment
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.state.UIState
import com.example.lib_common.utils.ToastUtils
import com.example.lib_storage.AuthMMKV
import com.example.module_motorcircle.R
import com.example.module_motorcircle.motor_circle.adapter.MotorCirclePagerAdapter
import com.example.module_motorcircle.databinding.FragmentMotirCircleBinding
import com.example.module_motorcircle.motor_circle.viewmodel.MotorCircleViewModel
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint


@Route(path = Router.MOTOR_MOTOR_FRAGMENT)
@AndroidEntryPoint
class MotorCircleFragment : BaseFragment<MotorCircleViewModel,FragmentMotirCircleBinding>(){
    override val mLayoutResId: Int
        get() = R.layout.fragment_motir_circle
    override val mViewModel: MotorCircleViewModel by viewModels()

    private var mAdapter: MotorCirclePagerAdapter? = null
    override fun initView() {

    }

    override fun initData() {
        mViewModel.getNavigationItem()

    }

    override fun initListener() {
        mViewModel.navigationState_.observeWithLifecycle(this){
            when(it){
                is UIState.Success -> {
                    handleNavigationData()
                }
                else -> {

                }
            }
        }
        mBinding.ivPublish.setOnClickListener {
            if (!AuthMMKV.hasLogin()){
                ToastUtils.showLong(requireActivity(),"请先登录")
                return@setOnClickListener
            }
            //跳转到发布帖子页面
            ARouter.getInstance().build(Router.MOTOR_CIRCLE_PUBLISH_POST_ACTIVITY).navigation()
        }
    }

    /**
     * 展示tab
     */
    private fun handleNavigationData() {
        mAdapter = MotorCirclePagerAdapter(mViewModel.tabs,this)
        mBinding.vp.adapter = mAdapter
        mBinding.vp.currentItem = 0 //默认显示第一
//        mBinding.tabLayout.setupWithViewPager(mBinding.vp) //和viewpager绑定
        TabLayoutMediator(mBinding.tabLayout,mBinding.vp){tab,position ->
            tab.text = mViewModel.tabs[position].title
            //....其他样式
        }.attach()
    }


}